﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.SystemManage.user
{
    /// <summary>
    /// 用户基本信息操作
    /// </summary>
    public partial class usergrid : AdminPage
    {
        /// <summary>
        /// 管理类型，默认0为全部，1为超级管理员，2为普通管理员，3为用户。
        /// </summary>
        protected int manageType = SASRequest.GetInt("mtype", 0);
        protected int groupid = 0;
        protected string parmStr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            string strWhere = "";

            if (ViewState["condition"] == null)
            {
                if (parmStr != "") strWhere = parmStr;
            }
            else
            {
                if (parmStr != "") strWhere = ViewState["condition"].ToString() + " AND " + parmStr;
                else strWhere = ViewState["condition"].ToString();
            }

            DataGrid1.VirtualItemCount = Users.GetUsersCount(strWhere);
            DataGrid1.DataSource = Users.GetUsersByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, strWhere, "");
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            string conditionstr = SASRequest.GetString("corpname");
            if (conditionstr != "" && Utils.IsSafeSqlString(conditionstr))
            {
                ViewState["condition"] = "lname like '%" + conditionstr + "%'";
            }
            else
            {
                ViewState["condition"] = "";
            }
            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 停止用户
        /// </summary>
        protected void pause_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Users.PauseUsers(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 开启用户
        /// </summary>
        protected void start_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Users.StartUsers(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 密码重置
        /// </summary>
        protected void reuserpass_ServerClick(object sender, EventArgs e)
        {
            int ruserid = SASRequest.GetInt("ruserid", 0);

            UserInfo ruinfo = Users.GetUser(ruserid);

            if (ruinfo != null && ruinfo.id > 0)
            {
                if (ruinfo.syspass == "")
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "初始密码为空，不能进行密码重置！");
                    return;
                }

                ruinfo.pass = Utils.MD5(ruinfo.syspass);
                if (Users.UpdateUser(ruinfo))
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "重置成功！");
                }
            }

            //OpLogs.CreateCompanyOPLog(userinfo, SASRequest.GetIP(), LTJ.Entity.OpAction.CompanyUserInfoUpdate.ActionID, "重置会员密码-会员名：" + ruinfo.lname);

            BindDataGrid();
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            string headname = "用户信息列表";
            if (manageType == 1)
            {
                headname = "超级管理员信息列表";
                parmStr = "id = manager";
                add.Visible = false;
                groupid = Convert.ToInt16(ManageGroup.AdminGroup);
            }
            else if (manageType == 2)
            {
                headname = "企业普通管理员信息列表";
                groupid = Convert.ToInt16(ManageGroup.CorpGroup);
                parmStr = "(id <> manager or manager is null) AND gid = " + groupid;
            }
            else if (manageType == 3)
            {
                groupid = Convert.ToInt16(ManageGroup.NormalGroup);
                parmStr = "(id <> manager or manager is null) AND gid = " + groupid;
            }

            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = headname;
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            check.Click += new EventHandler(check_Click);
            reclick.ServerClick += new EventHandler(reclick_Click);
            pause.Click += new EventHandler(pause_Click);
            start.Click += new EventHandler(start_Click);
            reuserpass.ServerClick += new EventHandler(reuserpass_ServerClick);
        }

        #endregion
    }
}